package chaw.android.util;

import chaw.android.train.R;
import android.app.Activity;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

public class FullScreenImage extends Activity{
	private ImageView fullScreenImage;
	private Bitmap bitMap;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.full_screen_image);
		fullScreenImage = (ImageView) findViewById(R.id.fullScreenImage);
		fullScreenImage.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Bundle data = getIntent().getExtras();
		String actionType = data.getString("type");
		
		if(actionType.equals("url")){
			String imageURL = data.getString("image");
			DownloadAsynkTask task = new DownloadAsynkTask();
			task.execute(new String[] {imageURL});
		}
		else if(actionType.equals("uri")){
			String imageURI = data.getString("image");
			fullScreenImage.setImageURI(Uri.parse(imageURI));
			fullScreenImage.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
		}
		else{
			Toast.makeText(getBaseContext(), "잘못된 경로입니다.", Toast.LENGTH_SHORT).show();
			finish();
		}
	}
	
	private class DownloadAsynkTask extends AsyncTask<String, Void, String> {
		@Override
		protected String doInBackground(String... urls) {
			String response = null;
			bitMap = LoadBitMap.fromURL(urls[0]);
			response = bitMap.getWidth() + "/" + bitMap.getHeight();
			return response;
		}
		
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			fullScreenImage.setImageBitmap(bitMap);
			fullScreenImage.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
		}
	}
}
